//
//  SelectPictureViewController.m
//  MagictoLane
//
//  Created by 锋 on 2017/12/30.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "SelectPictureViewController.h"
#import "BomImgsModel.h"
#import "TZImagePickerController.h"

@interface SelectPictureViewController ()<UITableViewDataSource, UITableViewDelegate, TZImagePickerControllerDelegate>
@property (nonatomic, strong) UITableView *dataPictureTableView;

@end

@implementation SelectPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupFillDataTableView];
    // Do any additional setup after loading the view.
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.selectImageBlock(_imageArr);
}

- (void)setupUI {
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tianjia"] style:UIBarButtonItemStylePlain target:self action:@selector(pickImageFromAlbum)];
    [rightBtn setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = _pictureType == SELECTPACKIMAGETYPE ? @"指定包装" : @"Bom资料";
    self.view.backgroundColor = RGB(242, 242, 242);
}

- (void)setupFillDataTableView {
    _dataPictureTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _dataPictureTableView.delegate = self;
    _dataPictureTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _dataPictureTableView.tableHeaderView = view;
    _dataPictureTableView.tableFooterView = view;
    _dataPictureTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_dataPictureTableView];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _imageArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = _imageArr[indexPath.row];
    return (SCREEN_WIDTH - 10)/image.size.width*image.size.height + 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImage *image = _imageArr[indexPath.row];
    UIImageView *pictureImageView = [ZCControl createImageViewWithFrame:CGRectMake(5, 5, SCREEN_WIDTH - 10, (SCREEN_WIDTH - 10)/image.size.width*image.size.height) ImageName:nil];
    pictureImageView.image = image;
    [cell.contentView addSubview:pictureImageView];
    UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(pictureImageView.frame.size.width - 20, 0, 20, 20)];
    deleteBtn.backgroundColor = [UIColor clearColor];
    [deleteBtn addTarget:self action:@selector(onClickDeleteBtn:) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setImage:[UIImage imageNamed:@"shanchu"] forState:UIControlStateNormal];
    deleteBtn.tag = indexPath.row;
    [pictureImageView addSubview:deleteBtn];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)onClickDeleteBtn:(UIButton *)sender {
    [_imageArr removeObjectAtIndex:sender.tag];
    [_dataPictureTableView reloadData];
}

- (void)pickImageFromAlbum {
    TZImagePickerController *tzImagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:100000 delegate:self];
    tzImagePicker.maxImagesCount = 100000 - _imageArr.count;
    tzImagePicker.allowTakePicture = NO;
    tzImagePicker.allowPickingVideo = NO;
    [self presentViewController:tzImagePicker animated:YES completion:nil];
}

// MARK:TZImagePickerController
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto
{
    [self.imageArr addObjectsFromArray:photos];
    [_dataPictureTableView reloadData];
}

//需求：相册里的视频隐藏不给选。(不知道后面会不会又要选视频先留着)
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
